#include "model/model_user.h"
#include "model/mysqlhelper.h"
#include <mysql/mysql.h>

// 单一user
int selectUserByid(MYSQL *conn, int id, USER_M *user) {
	if(user == NULL)
		return -1;

	char *sql = "select name,salt,passwd,last_dir_id from user_7th where id=?;";

	MYSQL_STMT *stmt;
	MYSQL_BIND inbind, outbind[4];

	if(initStmt(&stmt, conn, sql)) {
		printf("init stmt_preper error\n");
		return 1;
	}

	memset(&inbind, 0, sizeof(inbind));
	initinBindParams(&inbind, MYSQL_TYPE_LONG, (void *)&id, sizeof(int));
	// 绑定参数到预编译语句
	if(mysql_stmt_bind_param(stmt, &inbind)) {
		fprintf(stderr, "mysql_stmt_inbind_param() failed: %s\n", mysql_stmt_error(stmt));
		mysql_stmt_close(stmt);
		return -1;
	}

	// 执行预编译语句
	if(mysql_stmt_execute(stmt)) {
		fprintf(stderr, "mysql_stmt_execute() failed: %s\n", mysql_stmt_error(stmt));
		mysql_stmt_close(stmt);
		return 1;
	}

	// 初始化接收列的类型
	memset(outbind, 0, sizeof(outbind));
	initBindResult(outbind, MYSQL_TYPE_STRING, user->name, 255, NULL, NULL, NULL);
	initBindResult(outbind + 1, MYSQL_TYPE_STRING, user->salt, 255, NULL, NULL, NULL);
	initBindResult(outbind + 2, MYSQL_TYPE_STRING, user->passwd, 255, NULL, NULL, NULL);
	initBindResult(outbind + 3, MYSQL_TYPE_LONG, &user->last_dir_id, -1, NULL, NULL, NULL);

	if(mysql_stmt_bind_result(stmt, outbind)) {
		fprintf(stderr, " mysql_stmt_bind_result() failed\n");
		fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
		return -1;
	}

	// 处理接收数据
	if(mysql_stmt_store_result(stmt)) {
		fprintf(stderr, " mysql_stmt_store_result() failed\n");
		fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
		exit(0);
	}

	if(mysql_stmt_fetch(stmt)) {
		printf("mysql_stmt_fetch error\n");
		return -1;
	}

	mysql_stmt_close(stmt);
	return 0;
}

// 单一user
int selectUserByName(MYSQL *conn, char name[], USER_M *user) {
	if(user == NULL)
		return -1;

	char *sql =
		"select id,name,salt,passwd,last_dir_id from user_7th where name=?;";

	MYSQL_STMT *stmt;
	MYSQL_BIND inbind, outbind[5];

	if(initStmt(&stmt, conn, sql)) {
		printf("init stmt_preper error\n");
		return 1;
	}

	memset(&inbind, 0, sizeof(inbind));
	initinBindParams(&inbind, MYSQL_TYPE_VAR_STRING, (void *)name, strlen(name));
	// 绑定参数到预编译语句
	if(mysql_stmt_bind_param(stmt, &inbind)) {
		fprintf(stderr, "mysql_stmt_inbind_param() failed: %s\n", mysql_stmt_error(stmt));
		mysql_stmt_close(stmt);
		return -1;
	}

	// 执行预编译语句
	if(mysql_stmt_execute(stmt)) {
		fprintf(stderr, "mysql_stmt_execute() failed: %s\n", mysql_stmt_error(stmt));
		mysql_stmt_close(stmt);
		return 1;
	}

	// 初始化接收列的类型
	memset(outbind, 0, sizeof(outbind));
	initBindResult(outbind, MYSQL_TYPE_LONG, &user->id, -1, NULL, NULL, NULL);
	initBindResult(outbind + 1, MYSQL_TYPE_STRING, user->name, 255, NULL, NULL, NULL);
	initBindResult(outbind + 2, MYSQL_TYPE_STRING, user->salt, 255, NULL, NULL, NULL);
	initBindResult(outbind + 3, MYSQL_TYPE_STRING, user->passwd, 255, NULL, NULL, NULL);
	initBindResult(outbind + 4, MYSQL_TYPE_LONG, &user->last_dir_id, -1, NULL, NULL, NULL);

	if(mysql_stmt_bind_result(stmt, outbind)) {
		fprintf(stderr, " mysql_stmt_bind_result() failed\n");
		fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
		return -1;
	}

	// 处理接收数据
	if(mysql_stmt_store_result(stmt)) {
		fprintf(stderr, " mysql_stmt_store_result() failed\n");
		fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
		exit(0);
	}

	if(mysql_stmt_fetch(stmt)) {
		printf("mysql_stmt_fetch error\n");
		return -1;
	}

	mysql_stmt_close(stmt);
	return 0;
}

int verifyUser(MYSQL *conn, const USER_M *user, int *last_id) {
	if(user == NULL)
		return -1;

	char *sql =
		"select id from user_7th where name=? and passwd=? and is_deleted=0;";

	MYSQL_STMT *stmt;
	MYSQL_BIND inbind[2], outbind;
	int count = -1;

	if(initStmt(&stmt, conn, sql)) {
		printf("init stmt_preper error\n");
		return 1;
	}

	memset(inbind, 0, sizeof(inbind));
	initinBindParams(inbind, MYSQL_TYPE_VAR_STRING, (void *)user->name, strlen(user->name));
	initinBindParams(inbind + 1, MYSQL_TYPE_VAR_STRING, (void *)user->passwd, strlen(user->passwd));

	// 绑定参数到预编译语句
	if(mysql_stmt_bind_param(stmt, inbind)) {
		fprintf(stderr, "mysql_stmt_inbind_param() failed: %s\n", mysql_stmt_error(stmt));
		mysql_stmt_close(stmt);
		return -1;
	}


	// 执行预编译语句
	if(mysql_stmt_execute(stmt)) {
		fprintf(stderr, "mysql_stmt_execute() failed: %s\n", mysql_stmt_error(stmt));
		mysql_stmt_close(stmt);
		return 1;
	}

	// 初始化接收列的类型
	memset(&outbind, 1, sizeof(outbind));
	initBindResult(&outbind, MYSQL_TYPE_LONG, &count, -1, NULL, NULL, NULL);

	if(mysql_stmt_bind_result(stmt, &outbind)) {
		fprintf(stderr, " mysql_stmt_bind_result() failed\n");
		fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
		return -1;
	}

	// 处理接收数据
	if(mysql_stmt_store_result(stmt)) {
		fprintf(stderr, " mysql_stmt_store_result() failed\n");
		fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
		exit(0);
	}

	if(mysql_stmt_fetch(stmt)) {
		printf("mysql_stmt_fetch error\n");
		return -1;
	}

	mysql_stmt_close(stmt);
	*last_id = count;
	return count > 0 ? 0 : -1;
}

// 返回-1表示参数不正确,1是系统失败，不符合表约束、初始失败等
int insertUser(MYSQL *conn, const USER_M *user, int *user_id) {

	char *sql =
		"insert into user_7th(name,salt,passwd,last_dir_id) value(?,?,?,-1);";
	// ------->
	MYSQL_STMT *stmt;
	// 定义 MYSQL_inbind 结构体
	MYSQL_BIND inbind[3];

	if(initStmt(&stmt, conn, sql)) {
		printf("init stmt_preper error\n");
		return 1;
	}

	memset(inbind, 0, sizeof(inbind));
	initinBindParams(inbind, MYSQL_TYPE_VAR_STRING, (void *)user->name, strlen(user->name));
	initinBindParams(inbind + 1, MYSQL_TYPE_VAR_STRING, (void *)user->salt, strlen(user->salt));
	initinBindParams(inbind + 2, MYSQL_TYPE_VAR_STRING, (void *)user->passwd, strlen(user->passwd));

	// 绑定参数到预编译语句
	if(mysql_stmt_bind_param(stmt, inbind)) {
		fprintf(stderr, "mysql_stmt_inbind_param() failed: %s\n", mysql_stmt_error(stmt));
		mysql_stmt_close(stmt);
		return -1;
	}

	// 执行预编译语句
	if(mysql_stmt_execute(stmt)) {
		fprintf(stderr, "mysql_stmt_execute() failed: %s\n", mysql_stmt_error(stmt));
		mysql_stmt_close(stmt);
		return 1;
	}
	*user_id = mysql_stmt_insert_id(stmt);
	mysql_stmt_close(stmt);
	return 0;
}

int deleteUser(MYSQL *conn, const USER_M *user) {
	if(user == 0)
		return -1;
	char sql[1024] = {0};
	if(user->id != 0)
		sprintf(sql, "update user_7th set is_deleted = %d where id =%d;", 1, user->id);
	else {
		if(strlen(user->name))
			sprintf(sql, "update user_7th set is_deleted = %d where name= '%s';", 1, user->name);
		else
			return -1;
	}

	if(mysql_query(conn, sql)) {
		printf("mysql_query error:%s\n", mysql_error(conn));
		return -1;
	}
	if(mysql_affected_rows(conn) == 0)
		return -1;
	return 0;
}

// 附加
int updatePasswd(MYSQL *conn, const USER_M *user, char newsalt, char newpasswd[]) {
	return 0;
}

int updateLastDir_idByid(MYSQL *conn, int id, int new_dir_id) {
	if(new_dir_id <= 0)
		return -1;
	char sql[1024] = {0};
	sprintf(sql, "update user_7th set last_dir_id = %d where id =%d;", new_dir_id, id);
	if(mysql_query(conn, sql)) {
		printf("mysql_query error:%s\n", mysql_error(conn));
		return -1;
	}
	if(mysql_affected_rows(conn) == 0)
		return -1;
	return 0;
}

int updateLastDir_idByName(MYSQL *conn, char name[], int last_dir_id) {
	if(name == NULL)
		return -1;
	char sql[1024] = {0};
	sprintf(sql, "update user_7th set last_dir_id = %d where name='%s';", last_dir_id, name);
	if(mysql_query(conn, sql)) {
		printf("mysql_query error:%s\n", mysql_error(conn));
		return -1;
	}
	if(mysql_affected_rows(conn) == 0)
		return -1;
	return 0;
}